package com.xhu.util;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

// TODO: 2021/3/26 多个"="挨着的时候忽略
public class Hider {

    private List<StringBuilder> html;

    private Queue<EqualSign> equals = new LinkedList<>();

    public Hider(List<StringBuilder> html){
        this.html = html;
        this.setEquals();
    }

    /**
     * 记录所有”=“号的位置
     */
    private void setEquals(){
        // TODO: 2021/3/26 需要去除原本“=”两边存在的空格 
        for (int i = 0; i < this.html.size(); i++) {
            StringBuilder s = new StringBuilder(html.get(i));
            int row = -1;
            while ((row=s.indexOf("=")) > -1){
                this.equals.offer(new EqualSign(i, row));
                s.replace(row,row+1, "0");
            }
        }
    }

    /**
     *
     * @return 返回html文件中的“=”数量
     */
    public int getEqualCount(){
        return this.equals.size();
    }

    /**
     * 执行信息隐藏
     * @param hideMes
     * @return
     */
    public List<StringBuilder> hide(Queue<Character> hideMes){
        EqualSign es = null;
        while ((es=this.equals.peek()) != null){
            if(hideMes.peek() != null){
                inster(hideMes, this.html,this.equals, es, 0);
            }
            if(hideMes.peek() != null){
                inster(hideMes, this.html,this.equals, es, 1);
            }
            this.equals.poll();
        }
        return this.html;
    }

    public String deCode(String type){
        //Queue<Character> queue = new LinkedList<>();

        if (!this.getTpye().equals("0"+type)){
            return "false";
        }

        StringBuilder lens = new StringBuilder();
        for (int i = 0; i < HideHtml.LEN/2; i++) {
            EqualSign e = this.equals.poll();
            StringBuilder temp = this.html.get(e.getLine());
            char left = temp.charAt(e.getRow()-1);
            char right = temp.charAt(e.getRow()+1);
            lens.append(left==' '?'1':'0');
            lens.append(right==' '?'1':'0');
        }

        int len = Integer.parseInt(lens.toString(), 2);

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < (len+1)/2; i++) {
            EqualSign e = this.equals.poll();
            StringBuilder temp = this.html.get(e.getLine());
            char left = temp.charAt(e.getRow()-1);
            char right = temp.charAt(e.getRow()+1);
            result.append(left==' '?'1':'0');
            result.append(right==' '?'1':'0');
        }

        return result.substring(0,len);
    }

    private String getTpye(){
        StringBuilder result = new StringBuilder();

        EqualSign e = this.equals.poll();
        StringBuilder temp = this.html.get(e.getLine());
        char left = temp.charAt(e.getRow()-1);
        char right = temp.charAt(e.getRow()+1);
        result.append(left==' '?'1':'0');
        result.append(right==' '?'1':'0');

        return result.toString();
    }

    /**
     * 将二进制的密文插入到”=“号的两侧
     * @param hideMes
     * @param html
     * @param equals
     * @param es
     * @param i
     */
    private static void inster(Queue<Character> hideMes, List<StringBuilder> html,Queue<EqualSign> equals, EqualSign es, int i){
        Character bir = hideMes.poll();
        if(bir == '1'){
            //在指定位置插入空格
            StringBuilder content = html.get(es.getLine());
            content.insert(es.getRow() + i, ' ');
            html.set(es.getLine(), content);
            changeEqual(equals, es.getLine());
        }
    }

    /**
     * 实时更新 “=” 号的位置
     * @param equals
     * @param line
     */
    private static void changeEqual(Queue<EqualSign> equals, int line){
        for (EqualSign e: equals) {
            if(e.getLine() == line){
                e.setRows(e.getRow()+1);
            }
        }
    }
}
